﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frmViPham : DevExpress.XtraEditors.XtraForm
    {
        #region khai bao bien
        public string maViPham="";
        TextMessageBox box = new TextMessageBox();
        DataTable dt;
        ViPham_BLL vp_bll = new ViPham_BLL();
        private int status=1;
        private bool isOK = false;
        #endregion 

        #region Loadform

        public frmViPham()
        {
            InitializeComponent();
        }

        private void frmViPham_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion


        #region cac  chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion 

        #region Cac su kien

        private void frmViPham_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }

        private void rbBoiThuong_CheckedChanged(object sender, EventArgs e)
        {
            if (rbBoiThuong.Checked == true)
                status = 0;
        }

        private void rbPhieuphat_CheckedChanged(object sender, EventArgs e)
        {
            if (rbPhieuphat.Checked == true)
                status = 1;
        }
        #endregion

        #region ham viet them

        private void UpdateData()
        {
            if (checkValid())
            {
                ViPham_Pubic vp = new ViPham_Pubic();
                if (maViPham=="")
                {
                    vp.MaViPham = box.AutoID("VP",2,vp_bll.TaoBang("select MaViPham from DM_ViPham"));
                    vp.NoiDung = txtNoiDung.Text;
                    vp.HinhThucXuLy = txtHinhThuc.Text;
                    vp.MucPhat = box.ToDecimal(txtMucPhat.Text);
                    vp.Status = status;
                    vp.GhiChu = txtGhiChu.Text;
                    vp_bll.Insert(vp);

                    CloseForm();
                }
                else
                {
                    vp.MaViPham = maViPham;
                    vp.NoiDung = txtNoiDung.Text;
                    vp.HinhThucXuLy = txtHinhThuc.Text;
                    vp.MucPhat = box.ToDecimal(txtMucPhat.Text);
                    vp.Status = status;
                    vp.GhiChu = txtGhiChu.Text;
                    vp_bll.Update(vp);

                    CloseForm();
                }
            }
        }

        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
            SetInterface();
        }
        private void LoadData()
        {
            if (maViPham !="")
            {
                dt = vp_bll.TaoBang(box.sqlCheckExist("DM_ViPham", "MaViPham", maViPham));
                    DataRow dr=dt.Rows[0];
                txtMaViPham.Text=dr["MaViPham"].ToString();
                txtNoiDung.Text=dr["NoiDung"].ToString();
                txtHinhThuc.Text=dr["HinhThucXuLy"].ToString();
                txtMucPhat.Text=dr["MucPhat"].ToString();
                txtGhiChu.Text=dr["GhiChu"].ToString();
                status=box.ToInt(dr["Status"].ToString());
                if(status==0)
                {
                    rbBoiThuong.Checked=true;
                    rbPhieuphat.Checked=false;
                }
                else 
                {
                    rbBoiThuong.Checked=false;
                    rbPhieuphat.Checked=true;
                }
                
            }

        }
        private bool checkValid()
        {
            if (txtNoiDung.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Nội dung.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtNoiDung.Focus();
                return false;
            }
            if (txtMucPhat.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Mức phạt.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtMucPhat.Focus();
                return false;
            }
            return true;
        }

        private void SetInterface()
        {
            box.Huy_Text(this);
            maViPham ="";
            txtMaViPham.Text = "Mã tự sinh";
            txtNoiDung.Focus();
        }
        #endregion
    }
}